package com.tenbent.product.base.utils;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 * 
 * 说明: 获取属性文件方法 方法一:
 * PropertiesUtil.class.getClassLoader().getResourceAsStream(propName); 方法二:
 * PropertiesUtil.class.getResourceAsStream(propName); 要求
 * PropertiesUtils类与properties在同一目录下 方法三:
 * context.getResourceAsStream("/WEB-INF/classes/init.properties");
 * 对于Web工程也可以这样。
 */
public class PropertiesUtil {

	public static String readProperties(String propName, String key) throws Exception {

		String value;
		Properties prop = new Properties();
		InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(propName);
		prop.load(in);
		value = prop.getProperty(key);
		return value;
	}

	public static void writeProperties(String propName, String paramName, String paramValue) throws Exception {

		Properties prop = new Properties();
		InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(propName);
		prop.load(in);
		// 此路径为class对应的路径应在WEB-INF/class
		OutputStream out = new FileOutputStream(PropertiesUtil.class.getClassLoader().getResource(propName).getFile());
		prop.setProperty(paramName, paramValue);
		prop.store(out, "Update '" + paramName + "' value " + paramValue);
	}

	// public static void main(String[] args) {
	//
	// String filePath = "mybatis.properties";
	// try {
	// String basePackage = PropertiesUtil.readProperties(filePath,
	// "mybatis.scan.package");
	// System.out.println("basePackage: "+basePackage);
	// PropertiesUtil.writeProperties(filePath, "yan", "wei");
	// System.out.println("value--->"+PropertiesUtil.readProperties(filePath,
	// "yan"));
	// } catch (Exception e) {
	//
	// e.printStackTrace();
	// }
	// }
}
